#include "tmsessionmanager.h"


void ClientSessionManagerI::remove_session(ClientSessionI *pSession)
{
    IceUtil::Mutex::Lock lock(m_MapMutex);

    std::map<ClientSessionI*, std::string>::iterator it;
    it = m_mapSession.find(pSession);
    if( it!=m_mapSession.end() ){
        m_mapSession.erase(pSession);
    }
}

void ClientSessionManagerI::list_session(Ice::StringSeq &strList)
{
    IceUtil::Mutex::Lock lock(m_MapMutex);

    std::map<ClientSessionI*, std::string>::iterator it;
    for(it=m_mapSession.begin();it!=m_mapSession.end();it++)
    {
//        qDebug()<<"ClientSessionManagerI::list_session"<<it->second.c_str();
        strList.push_back(it->second);
    }
}

Glacier2::SessionPrx ClientSessionManagerI::create(const std::string &userId, const Glacier2::SessionControlPrx &sPrx, const Ice::Current &current)
{
    IceUtil::Mutex::Lock lock(m_MapMutex);

    Ice::Identity ident = { IceUtil::generateUUID(), "session" };
    ClientSessionI* pClientSession = new ClientSessionI(this);
    m_mapSession.insert(std::make_pair(pClientSession, userId));
    return Glacier2::SessionPrx::uncheckedCast(current.adapter->add(pClientSession, ident));

}

ClientSessionI::ClientSessionI(const ClientSessionManagerI *pMgr)
{
    m_sessionMgr = (ClientSessionManagerI*)pMgr;
    qDebug()<<"[ClientSessionI]: create";
}

void ClientSessionI::ice_ping(const Ice::Current &cur) const
{
    qDebug()<<"[ClientSessionI]: ice_ping";
}

void ClientSessionI::destroy(const Ice::Current &cur)
{
    m_sessionMgr->remove_session(this);
    qDebug()<<"[ClientSessionI]: destroy";
}

